<?php
/**
 * 函数封装
 */
    $path = "./pic/";//文件上传的路径；
    $fileType = array("image/jpeg","image/png","image/gif");
    $maxSize = 0;//表示不限制；
    $upfile = $_FILES['upic'];
    //调用函数；
//    var_dump(upload($path,$fileType,$maxSize,$upfile));
//缩放
function fun($maxw,$maxh,$path1,$pic){
    $str = getimagesize($path1.$pic);
    switch($str[2]){
        case 1:
            $im = imagecreatefromgif($path1.$pic);
            break;
        case 2:
            $im = imagecreatefromjpeg($path1.$pic);
            break;
        case 3:
            $im = imagecreatefrompng($path1.$pic);
            break;
    }
    $w = imagesx($im);
    $h = imagesy($im);
    if($w>$h){
        $max = $w/$maxw;
        $ww = $w/$max;
        $hh = $h/$max;
    }else{
        $max = $h/$maxh;
        $ww = $w/$max;
        $hh = $h/$max;
    }
    $img = imagecreatetruecolor($ww,$hh);
    imagecopyresampled($img,$im,0,0,0,0,$ww,$hh,$w,$h);
//    $y = date("Ymdhis");
    switch($str[2]){
        case 1:
            imagegif($img,"./pic/suolue/{$pic}");
            break;
        case 2:
            imagejpeg($img,"./pic/suolue/{$pic}");
            break;
        case 3:
            imagepng($img,"./pic/suolue/{$pic}");
            break;
    }
    imagedestroy($im);
    imagedestroy($img);
}


//执行
    function upload($path,$fileType,$maxSize=0,$upfile)
    {
        $res =array(
            "info"=>"",
            "error"=>false
        );
        if(empty($upfile)){
            $res['info']="文件不能为空";
            return $res;
        }
        if(!in_array($upfile['type'],$fileType)){
            $res['info']="文件信息有误";
            return $res;
        }
        if($upfile['size']<$maxSize){
            $res['info']="超出文件可上传的大小";
            return $res;
        }
        if($upfile['error']>0){
            switch($upfile['error']){
                case 1:
                    $info ="上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值";
                    break;
                case 2:
                    $info = "表示上传文件大小超出了HTML表单隐藏域属性的MAX＿FILE＿SIZE元素所指定的最大值";
                    break;
                case 3:
                    $info = "表示文件只被部分上传";
                    break;
                case 4:
                    $info = "表示没有上传任何文件";
                    break;
                case 6:
                    $info = "找不到临时文件夹";
                    break;
                case 7:
                    $info = "文件写入失败";
                    break;
            }
            $res['info'] = $info;
            $res['error'] = $upfile['error'];
        }
        //随机产生一个文件名；
        $pathinfo = pathinfo($upfile['name']);
        $ext = $pathinfo['extension'];
        $path = rtrim($path,"/")."/";
        do{
            $newName = date("Ymd").rand(1000,9999).".".$ext ;
        }while(file_exists($path.$newName));
        if(is_uploaded_file($upfile['tmp_name'])){
            if(move_uploaded_file($upfile['tmp_name'],$path.$newName)){
                $res['info'] = "恭喜您文件上传成功，文件名是：".$newName;
                $res['error'] = true;
                //调用图片缩略
                $maxw = 150;
                $maxh = 150;
                $path1 = "./pic/";
                $pic1 = $newName;
                fun($maxw,$maxh,$path1,$pic1);
                //图片缩略调用结束
                return $res;
            }else{
                $res['info'] = "文件上传有误";
                return $res;
            }
        }else{
            $res['info'] = "不是上传文件";
            return $res;
        }
    }
